```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java线程机制深度解析 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', 'Noto Serif SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
            line-height: 1.6;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 50%, #d946ef 100%);
        }
        .code-block {
            background-color: #1e293b;
            border-radius: 0.5rem;
            overflow: hidden;
        }
        .code-header {
            background-color: #334155;
            padding: 0.5rem 1rem;
            color: #e2e8f0;
            font-family: monospace;
            display: flex;
            justify-content: space-between;
        }
        .code-body {
            padding: 1rem;
            color: #f8fafc;
            font-family: monospace;
            white-space: pre;
            overflow-x: auto;
        }
        .highlight {
            background-color: rgba(139, 92, 246, 0.1);
            padding: 0.15rem 0.3rem;
            border-radius: 0.25rem;
            color: #7c3aed;
            font-weight: 500;
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .state-badge {
            display: inline-block;
            padding: 0.25rem 0.75rem;
            border-radius: 9999px;
            font-weight: 500;
            font-size: 0.875rem;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">Java线程机制深度解析</h1>
                    <p class="text-xl mb-8 opacity-90">探索并发编程的核心原理与最佳实践</p>
                    <div class="flex items-center space-x-4">
                        <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full flex items-center">
                            <i class="fas fa-laptop-code mr-2"></i>
                            <span>并发编程</span>
                        </span>
                        <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full flex items-center">
                            <i class="fas fa-layer-group mr-2"></i>
                            <span>JVM底层</span>
                        </span>
                    </div>
                </div>
                <div class="md:w-1/2 flex justify-center">
                    <div class="w-64 h-64 md:w-80 md:h-80 relative">
                        <div class="absolute inset-0 bg-indigo-500 rounded-2xl opacity-20 animate-pulse"></div>
                        <div class="absolute inset-4 bg-purple-500 rounded-xl opacity-20 animate-pulse delay-100"></div>
                        <div class="relative bg-white bg-opacity-10 backdrop-blur-sm rounded-3xl p-6 shadow-xl border border-white border-opacity-20">
                            <div class="text-center">
                                <i class="fas fa-code-branch text-5xl mb-4 text-white"></i>
                                <h3 class="text-xl font-semibold mb-2">线程生命周期</h3>
                                <div class="grid grid-cols-2 gap-2">
                                    <span class="state-badge bg-yellow-500 bg-opacity-20">NEW</span>
                                    <span class="state-badge bg-green-500 bg-opacity-20">RUNNABLE</span>
                                    <span class="state-badge bg-red-500 bg-opacity-20">BLOCKED</span>
                                    <span class="state-badge bg-blue-500 bg-opacity-20">WAITING</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="prose prose-lg max-w-none">
                <p class="text-xl text-slate-600 mb-8">
                    在Java中，线程的创建与管理是并发编程的核心内容，理解其背后的实现机制有助于更高效地进行多线程开发。Java通过<code class="highlight">Thread</code>类和<code class="highlight">Executor</code>框架提供了线程的创建与管理功能。
                </p>
            </div>
        </section>

        <!-- Section 1: Thread Creation -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="bg-indigo-100 text-indigo-800 w-10 h-10 rounded-full flex items-center justify-center mr-4">1</span>
                线程的创建与启动流程
            </h2>
            
            <p class="text-lg text-slate-600 mb-6">线程的创建可以通过两种方式：继承<code class="highlight">Thread</code>类和实现<code class="highlight">Runnable</code>接口。下面我们详细分析这两种方式的实现。</p>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <!-- 1.1 Inherit Thread -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition-all duration-300">
                    <div class="p-6">
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-arrow-right text-indigo-500 mr-2"></i>
                            继承Thread类创建线程
                        </h3>
                        <p class="text-slate-600 mb-4">继承<code class="highlight">Thread</code>类创建线程时，首先需要重写<code class="highlight">run()</code>方法，然后通过调用<code class="highlight">start()</code>方法来启动线程。调用<code class="highlight">start()</code>方法时，JVM会创建一个新的操作系统级线程来执行<code class="highlight">run()</code>方法。</p>
                        
                        <div class="code-block mb-4">
                            <div class="code-header">
                                <span>MyThread.java</span>
                                <button class="text-blue-300 hover:text-white">
                                    <i class="far fa-copy"></i>
                                </button>
                            </div>
                            <div class="code-body">
                                <code>
public class MyThread extends Thread {
    @Override
    public void run() {
        // 线程执行的代码
        System.out.println("Thread is running...");
    }

    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();  // 启动线程
    }
}</code>
                            </div>
                        </div>
                        
                        <div class="space-y-3">
                            <div class="flex items-start">
                                <span class="bg-indigo-100 text-indigo-800 rounded-full w-6 h-6 flex items-center justify-center mr-2 mt-1 flex-shrink-0">+</span>
                                <div>
                                    <p class="font-medium">start()方法</p>
                                    <p class="text-sm text-slate-500"><code class="highlight">start()</code>方法会在调用时由JVM触发，调用后会将当前线程状态从<code class="highlight">NEW</code>变为<code class="highlight">RUNNABLE</code>，然后进入调度队列等待CPU资源。</p>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <span class="bg-indigo-100 text-indigo-800 rounded-full w-6 h-6 flex items-center justify-center mr-2 mt-1 flex-shrink-0">+</span>
                                <div>
                                    <p class="font-medium">start0()</p>
                                    <p class="text-sm text-slate-500">该方法为JVM中的本地方法，负责启动底层的操作系统线程。具体实现依赖于不同的操作系统和平台。</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 1.2 Implement Runnable -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition-all duration-300">
                    <div class="p-6">
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-arrow-right text-purple-500 mr-2"></i>
                            实现Runnable接口创建线程
                        </h3>
                        <p class="text-slate-600 mb-4">通过实现<code class="highlight">Runnable</code>接口并将其传递给<code class="highlight">Thread</code>对象，Java允许线程任务的复用，即同一个任务可以在多个线程中执行。与继承<code class="highlight">Thread</code>类相比，实现<code class="highlight">Runnable</code>接口的方式更加灵活。</p>
                        
                        <div class="code-block mb-4">
                            <div class="code-header">
                                <span>MyRunnable.java</span>
                                <button class="text-blue-300 hover:text-white">
                                    <i class="far fa-copy"></i>
                                </button>
                            </div>
                            <div class="code-body">
                                <code>
public class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("Thread is running via Runnable...");
    }

    public static void main(String[] args) {
        MyRunnable task = new MyRunnable();
        Thread thread = new Thread(task);  // 创建Thread对象并传入Runnable实例
        thread.start();  // 启动线程
    }
}</code>
                            </div>
                        </div>
                        
                        <div class="space-y-3">
                            <div class="flex items-start">
                                <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-2 mt-1 flex-shrink-0">+</span>
                                <div>
                                    <p class="font-medium">Thread构造器</p>
                                    <p class="text-sm text-slate-500"><code class="highlight">Thread</code>类的构造方法接收一个<code class="highlight">Runnable</code>对象，当调用<code class="highlight">start()</code>时，JVM会调用<code class="highlight">Runnable</code>的<code class="highlight">run()</code>方法。</p>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <span class="bg-purple-100 text-purple-800 rounded-full w-6 h-6 flex items-center justify-center mr-2 mt-1 flex-shrink-0">+</span>
                                <div>
                                    <p class="font-medium">Runnable.run()</p>
                                    <p class="text-sm text-slate-500">这里的<code class="highlight">run()</code>方法实际上是由线程调度来执行的，当线程被启动时，JVM会执行该方法，完成实际的任务。</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 2: Thread States -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="bg-indigo-100 text-indigo-800 w-10 h-10 rounded-full flex items-center justify-center mr-4">2</span>
                线程的状态与生命周期管理
            </h2>
            
            <p class="text-lg text-slate-600 mb-6">Java线程的生命周期由<code class="highlight">Thread</code>类管理，线程在生命周期中经历多个状态。Java通过<code class="highlight">Thread.State</code>枚举类定义了线程的生命周期状态。</p>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-sitemap text-indigo-500 mr-2"></i>
                        线程的不同状态
                    </h3>
                    <p class="text-slate-600 mb-6">Java线程的生命周期包括以下状态：</p>
                    
                    <div class="space-y-3 mb-8">
                        <div class="flex items-center">
                            <span class="state-badge bg-yellow-500 bg-opacity-20 mr-3">NEW</span>
                            <span class="text-slate-600">线程刚被创建，尚未启动。</span>
                        </div>
                        <div class="flex items-center">
                            <span class="state-badge bg-green-500 bg-opacity-20 mr-3">RUNNABLE</span>
                            <span class="text-slate-600">线程已启动，等待操作系统调度执行。</span>
                        </div>
                        <div class="flex items-center">
                            <span class="state-badge bg-red-500 bg-opacity-20 mr-3">BLOCKED</span>
                            <span class="text-slate-600">线程由于竞争锁而被阻塞。</span>
                        </div>
                        <div class="flex items-center">
                            <span class="state-badge bg-blue-500 bg-opacity-20 mr-3">WAITING</span>
                            <span class="text-slate-600">线程等待其他线程的通知。</span>
                        </div>
                        <div class="flex items-center">
                            <span class="state-badge bg-indigo-500 bg-opacity-20 mr-3">TIMED_WAITING</span>
                            <span class="text-slate-600">线程在特定时间段内等待。</span>
                        </div>
                        <div class="flex items-center">
                            <span class="state-badge bg-gray-500 bg-opacity-20 mr-3">TERMINATED</span>
                            <span class="text-slate-600">线程执行完毕或被终止。</span>
                        </div>
                    </div>
                    
                    <div class="code-block mb-4">
                        <div class="code-header">
                            <span>Thread.State</span>
                        </div>
                        <div class="code-body">
                            <code>
public enum State {
    NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED;
}</code>
                        </div>
                    </div>
                </div>
                
                <div>
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-exchange-alt text-indigo-500 mr-2"></i>
                        线程的状态转换
                    </h3>
                    <p class="text-slate-600 mb-6">线程的状态转换涉及方法调用，例如：</p>
                    
                    <div class="mermaid mb-6">
                        stateDiagram-v2
                            [*] --> NEW
                            NEW --> RUNNABLE: start()
                            RUNNABLE --> BLOCKED: 等待锁
                            BLOCKED --> RUNNABLE: 获取锁
                            RUNNABLE --> WAITING: wait()
                            WAITING --> RUNNABLE: notify()
                            RUNNABLE --> TIMED_WAITING: sleep()
                            TIMED_WAITING --> RUNNABLE: 超时
                            RUNNABLE --> TERMINATED: 执行完成
                    </div>
                    
                    <div class="bg-white rounded-lg p-6 shadow-inner border border-gray-200">
                        <p class="text-sm text-slate-600 mb-4"><i class="fas fa-info-circle text-blue-500 mr-2"></i>线程的状态变化是在<code class="highlight">Thread</code>类的管理下通过各种同步方法（如<code class="highlight">synchronized</code>）和系统调用来实现的。</p>
                        
                        <div class="code-block">
                            <div class="code-header">
                                <span>Thread.start()</span>
                            </div>
                            <div class="code-body">
                                <code>
public synchronized void start() {
    if (state != NEW)
        throw new IllegalThreadStateException();
    state = RUNNABLE;  // 将线程状态设置为RUNNABLE
    start0();  // 调用操作系统方法启动线程
}</code>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 3: Thread Scheduling -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="bg-indigo-100 text-indigo-800 w-10 h-10 rounded-full flex items-center justify-center mr-4">3</span>
                线程的调度与执行流程
            </h2>
            
            <p class="text-lg text-slate-600 mb-6">线程的调度是由JVM和操作系统共同控制的。Java提供了多种方法来影响线程的执行优先级、线程的睡眠等行为。</p>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-tasks text-indigo-500 mr-2"></i>
                        线程调度机制
                    </h3>
                    <p class="text-slate-600 mb-4">线程调度的核心是操作系统的调度器，它会根据线程的优先级和其他策略来决定哪个线程获得CPU资源。Java允许通过<code class="highlight">Thread.setPriority()</code>方法来设置线程的优先级。</p>
                    
                    <div class="code-block mb-6">
                        <div class="code-header">
                            <span>Thread.setPriority()</span>
                        </div>
                        <div class="code-body">
                            <code>
public void setPriority(int newPriority) {
    if (newPriority < MIN_PRIORITY || newPriority > MAX_PRIORITY)
        throw new IllegalArgumentException();
    this.priority = newPriority;
}</code>
                        </div>
                    </div>
                    
                    <div class="bg-blue-50 border-l-4 border-blue-500 p-4">
                        <p class="font-medium text-blue-800">线程优先级</p>
                        <p class="text-sm text-blue-700">线程优先级是操作系统调度时参考的一个指标，高优先级线程会相对早些被调度执行。</p>
                    </div>
                </div>
                
                <div>
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-clock text-indigo-500 mr-2"></i>
                        sleep()与yield()的使用
                    </h3>
                    
                    <div class="space-y-6">
                        <div>
                            <p class="text-slate-600 mb-2"><code class="highlight">sleep()</code>：让当前线程在指定时间内进入休眠状态，进入<code class="highlight">TIMED_WAITING</code>状态，允许操作系统调度其他线程。</p>
                            
                            <div class="code-block">
                                <div class="code-header">
                                    <span>Thread.sleep()</span>
                                </div>
                                <div class="code-body">
                                    <code>
public static void sleep(long millis) throws InterruptedException {
    Thread.sleep0(millis);
}</code>
                                </div>
                            </div>
                        </div>
                        
                        <div>
                            <p class="text-slate-600 mb-2"><code class="highlight">yield()</code>：让当前线程主动让出CPU资源，进入<code class="highlight">RUNNABLE</code>状态，允许同优先级的线程获得CPU时间。</p>
                            
                            <div class="code-block">
                                <div class="code-header">
                                    <span>Thread.yield()</span>
                                </div>
                                <div class="code-body">
                                    <code>
public static void yield() {
    // 调用操作系统调度接口，线程主动放弃CPU资源
    sun.misc.Unsafe.getUnsafe().yield();
}</code>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 4: Thread Pool -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="bg-indigo-100 text-indigo-800 w-10 h-10 rounded-full flex items-center justify-center mr-4">4</span>
                线程池的管理
            </h2>
            
            <p class="text-lg text-slate-600 mb-6">线程池是Java中管理大量线程的重要工具，避免频繁创建和销毁线程的开销。Java通过<code class="highlight">Executor</code>框架来管理线程池，其中最常用的是<code class="highlight">ThreadPoolExecutor</code>类。</p>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div>
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-project-diagram text-indigo-500 mr-2"></i>
                        ThreadPoolExecutor类实现
                    </h3>
                    <p class="text-slate-600 mb-4"><code class="highlight">ThreadPoolExecutor</code>类是<code class="highlight">ExecutorService</code>接口的实现，它管理着线程池中的线程，并通过任务队列来调度线程。线程池通过指定核心线程数、最大线程数、任务队列等来管理线程的创建和销毁。</p>
                    
                    <div class="code-block mb-6">
                        <div class="code-header">
                            <span>ThreadPoolExecutor</span>
                        </div>
                        <div class="code-body">
                            <code>
public class ThreadPoolExecutor extends AbstractExecutorService {
    private final BlockingQueue<Runnable> workQueue;
    private volatile int poolSize;
    
    public void execute(Runnable command) {
        if (poolSize < corePoolSize) {
            addWorker(command);  // 如果线程池内线程数小于核心线程数，则创建新线程执行任务
        } else {
            workQueue.offer(command);  // 否则将任务加入任务队列
        }
    }
}</code>
                        </div>
                    </div>
                    
                    <div class="space-y-3">
                        <div class="flex items-start">
                            <span class="bg-indigo-100 text-indigo-800 rounded-full w-6 h-6 flex items-center justify-center mr-2 mt-1 flex-shrink-0">+</span>
                            <div>
                                <p class="font-medium">addWorker()</p>
                                <p class="text-sm text-slate-500">在需要时增加工作线程。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <span class="bg-indigo-100 text-indigo-800 rounded-full w-6 h-6 flex items-center justify-center mr-2 mt-1 flex-shrink-0">+</span>
                            <div>
                                <p class="font-medium">workQueue</p>
                                <p class="text-sm text-slate-500">使用阻塞队列存储待执行的任务，当线程池中有空闲线程时，任务会被取出执行。</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div>
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-sliders-h text-indigo-500 mr-2"></i>
                        线程池的参数与调优
                    </h3>
                    <p class="text-slate-600 mb-4"><code class="highlight">ThreadPoolExecutor</code>构造函数允许我们传递不同的参数来调节线程池的行为：</p>
                    
                    <div class="bg-white rounded-lg p-6 shadow-md mb-6">
                        <div class="grid grid-cols-2 gap-4">
                            <div>
                                <p class="font-medium text-indigo-700">corePoolSize</p>
                                <p class="text-sm text-slate-600">核心线程池大小，核心线程数总是保持活跃状态。</p>
                            </div>
                            <div>
                                <p class="font-medium text-indigo-700">maximumPoolSize</p>
                                <p class="text-sm text-slate-600">最大线程池大小，当任务量过大时，线程池会创建更多的线程直到达到最大线程数。</p>
                            </div>
                            <div>
                                <p class="font-medium text-indigo-700">keepAliveTime</p>
                                <p class="text-sm text-slate-600">空闲线程的存活时间。</p>
                            </div>
                            <div>
                                <p class="font-medium text-indigo-700">workQueue</p>
                                <p class="text-sm text-slate-600">任务队列，用于存放待执行的任务。</p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="code-block">
                        <div class="code-header">
                            <span>ThreadPoolExecutor构造器</span>
                        </div>
                        <div class="code-body">
                            <code>
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
                          long keepAliveTime, TimeUnit unit,
                          BlockingQueue<Runnable> workQueue) {
    this.corePoolSize = corePoolSize;
    this.maximumPoolSize = maximumPoolSize;
    this.workQueue = workQueue;
}</code>
                        </div>
                    </div>
                    
                    <p class="text-sm text-slate-500 mt-4"><i class="fas fa-lightbulb text-yellow-500 mr-2"></i><strong>线程池的工作原理</strong>：任务会被提交到队列，如果线程池中的线程数低于<code class="highlight">corePoolSize</code>，就会创建新线程执行任务。如果任务队列满了，且线程数未达到最大值，则会创建新的线程来处理任务。</p>
                </div>
            </div>
        </section>

        <!-- Section 5: Thread Termination -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="bg-indigo-100 text-indigo-800 w-10 h-10 rounded-full flex items-center justify-center mr-4">5</span>
                线程的终止与资源回收
            </h2>
            
            <p class="text-lg text-slate-600 mb-6">线程在执行完任务后，需要进行资源回收。Java的线程终止通常通过<code class="highlight">Thread.interrupt()</code>方法来实现。</p>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-stop-circle text-indigo-500 mr-2"></i>
                        interrupt()方法的实现
                    </h3>
                    <p class="text-slate-600 mb-4"><code class="highlight">interrupt()</code>方法用于中断线程的执行，当线程被中断时，会抛出<code class="highlight">InterruptedException</code>，线程可以在合适的地方捕获该异常，并结束自己的执行。</p>
                    
                    <div class="code-block mb-4">
                        <div class="code-header">
                            <span>Thread.interrupt()</span>
                        </div>
                        <div class="code-body">
                            <code>
public void interrupt() {
    interrupt0();
}

private native void interrupt0();</code>
                        </div>
                    </div>
                    
                    <div class="bg-blue-50 border-l-4 border-blue-500 p-4">
                        <p class="font-medium text-blue-800">interrupt()</p>
                        <p class="text-sm text-blue-700">通过底层操作系统调用中断线程的执行。</p>
                    </div>
                </div>
                
                <div>
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-recycle text-indigo-500 mr-2"></i>
                        线程资源的回收
                    </h3>
                    <p class="text-slate-600">当线程执行完任务后，会自动进入<code class="highlight">TERMINATED</code>状态，线程资源将被操作系统回收。在使用线程池时，线程池会自动管理线程的生命周期，避免频繁创建和销毁线程。</p>
                    
                    <div class="mt-6 bg-green-50 border-l-4 border-green-500 p-4">
                        <p class="font-medium text-green-800">最佳实践</p>
                        <p class="text-sm text-green-700">推荐使用线程池来管理线程生命周期，而不是直接创建和销毁线程，这样可以提高性能并减少资源消耗。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="bg-white rounded-xl shadow-md p-8 mb-16">
            <h2 class="text-2xl font-bold mb-6 flex items-center text-indigo-700">
                <i class="fas fa-star mr-3"></i>
                关键总结
            </h2>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="border-l-4 border-indigo-500 pl-4">
                    <h3 class="font-semibold mb-2">线程创建</h3>
                    <p class="text-sm text-slate-600">Java提供两种线程创建方式：继承<code class="highlight">Thread</code>类和实现<code class="highlight">Runnable</code>接口，后者更加灵活。</p>
                </div>
                <div class="border-l-4 border-purple-500 pl-4">
                    <h3 class="font-semibold mb-2">生命周期</h3>
                    <p class="text-sm text-slate-600">线程有6种状态：NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED。</p>
                </div>
                <div class="border-l-4 border-blue-500 pl-4">
                    <h3 class="font-semibold mb-2">线程调度</h3>
                    <p class="text-sm text-slate-600">操作系统负责实际调度，Java提供优先级设置和<code class="highlight">sleep()</code>/<code class="highlight">yield()</code>方法影响调度。</p>
                </div>
                <div class="border-l-4 border-green-500 pl-4">
                    <h3 class="font-semibold mb-2">线程池</h3>
                    <p class="text-sm text-slate-600"><code class="highlight">ThreadPoolExecutor</code>是线程管理的核心类，通过参数配置可优化性能。</p>
                </div>
                <div class="border-l-4 border-red-500 pl-4">
                    <h3 class="font-semibold mb-2">线程终止</h3>
                    <p class="text-sm text-slate-600">使用<code class="highlight">interrupt()</code>中断线程，线程池会自动管理线程资源回收。</p>
                </div>
                <div class="border-l-4 border-yellow-500 pl-4">
                    <h3 class="font-semibold mb-2">最佳实践</h3>
                    <p class="text-sm text-slate-600">优先使用线程池，避免直接创建线程；合理设置线程优先级；正确处理线程中断。</p>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-semibold">技术小馆</h3>
                    <p class="text-gray-400">探索技术之美，分享编程之道</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition duration-300 flex items-center">
                        <i class="fas fa-globe mr-2"></i>
                        <span>http://www.yuque.com/jtostring</span>
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-6 pt-6 text-center text-gray-400 text-sm">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true
            }
        });
        
        // Add copy functionality to code blocks
        document.querySelectorAll('.code-header button').forEach(button => {
            button.addEventListener('click', () => {
                const codeBlock = button.parentElement.nextElementSibling;
                const code = codeBlock.textContent;
                navigator.clipboard.writeText(code).then(() => {
                    const originalIcon = button.innerHTML;
                    button.innerHTML = '<i class="fas fa-check"></i>';
                    setTimeout(() => {
                        button.innerHTML = originalIcon;
                    }, 2000);
                });
            });
        });
    </script>
</body>
</html>
```